<?php
/**
 * 用户管理
 */

class UserModel extends Model {
	
	// 表名
	protected $tableName = 'user';
	
	// 主键
	protected $_primary = 'uid';
	
	/**
	 * 用户名登陆
	 *
	 * @param string $username        	
	 * @param string $password        	
	 */
	public function Login($username, $password) {
		if (empty ( $username )) {
			return array (
					'rtn_code' => - 1,
					'content' => '请输入用户名!' 
			);
		}
		
		if (empty ( $password )) {
			return array (
					'rtn_code' => - 1,
					'content' => '请输入密码!' 
			);
		}
	}
	
	/**
	 * 邮箱登陆
	 *
	 * @param string $email        	
	 * @param string $password        	
	 */
	public function LoginEmail($email, $password) {
		if (empty ( $email )) {
			return array (
					'rtn_code' => - 1,
					'content' => '请输入邮箱!' 
			);
		}
		
		if (empty ( $password )) {
			return array (
					'rtn_code' => - 1,
					'content' => '请输入密码!' 
			);
		}
	}
	
	/**
	 * 用户注册
	 *
	 * @param array $data        	
	 */
	public function Register($data) {
		if (! is_array ( $data )) {
			return array (
					'rtn_code' => - 1,
					'content' => '参数错误!' 
			);
		}
		
		if (empty ( $data ['username'] )) {
			return array (
					'rtn_code' => - 1,
					'content' => '登陆名不能为空!' 
			);
		}
		
		if (empty ( $data ['password'] )) {
			return array (
					'rtn_code' => - 1,
					'content' => '密码不能为空!' 
			);
		} else {
			$data ['password'] = md5 ( $data ['password'] );
		}
		
		if (! isset ( $data ['sex'] ) || ! is_numeric ( $data ['sex'] )) {
			$data ['sex'] = 0;
		}
		
		$data ['regip'] = get_client_ip ();
		$data ['lastip'] = get_client_ip ();
		$data ['ctime'] = date ( "Y-m-d H:i:s", time () );
		
		$rtn_code = 0;
		
		// 启动事务
		$this->startTrans ();
		
		$insertId = $this->data ( $data )->add ();
		
		if (empty ( $insertId ) || $insertId <= 0) {
			$rtn_code = - 1;
		}
		
		if ($rtn_code == 0) {
			// 提交事务
			$this->commit ();
			return array (
					'rtn_code' => 0,
					'content' => '用户注册成功!' 
			);
		} else {
			// 事务回滚
			$this->rollback ();
			return array (
					'rtn_code' => - 1,
					'content' => '系统错误，用户注册失败!' 
			);
		}
	}
}
?>